<template>
  <div class="modal" v-show="isShowModal">
    <div class="mask" @click="changeModal(false)"></div>
    <div class="loginBox">
      <div class="close" @click="changeModal(false)"></div>
      <section class="nav">
        <a
          href="#"
          :class="{ active: isPhoneLogin }"
          @click="isPhoneLogin = true"
          >手机号登录</a
        >
        |
        <a href="#" :class="{ active: !isPhoneLogin }" @click="showQR"
          >微信扫码登录</a
        >
      </section>
      <div class="content">
        <div class="phoneLogin" v-show="isPhoneLogin">
          <div class="row">
            <input
              type="text"
              placeholder="请输入手机号"
              v-model.trim="phoneNum"
            />
          </div>
          <div class="row">
            <slide-verify
              :l="42"
              :r="20"
              :w="362"
              :h="140"
              :accuracy="20"
              :imgs="arr"
              @success="onSuccess"
              @fail="onFail"
              @refresh="onRefresh"
              :style="{ width: '100%' }"
              class="slide-box"
              ref="slideBlock"
              :slider-text="msg"
            ></slide-verify>
          </div>
          <div class="row">
            <input type="text" placeholder="请输入验证码" v-model.trim="code" />
            <!-- 方法一 -->
            <!-- <button class="codeBtn" @click="getCode" :disabled="!isShowCount">
              {{ isShowCount ? "获取验证码" : count + "s" }}
            </button> -->
            <!-- 方法二 -->
            <button class="codeBtn" @click="getCode" v-show="isShowCount">
              获取验证码
            </button>
            <button class="codeBtn" v-show="!isShowCount">{{ count }}s</button>
          </div>
          <div class="row">
            <button class="loginBtn" @click="checkFn">登录</button>
          </div>
        </div>
        <div class="WeChatLogin" v-show="!isPhoneLogin" id="weixin">
          微信扫码登录
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import { mapMutations, mapState, mapActions } from "vuex";
import { sendSMS, phoneRegin, wxBindPhone } from "@/request/api";
import { checkPhoneNum } from "@/utils/index";
export default {
  data() {
    return {
      isPhoneLogin: true,
      msg: "向右滑动",
      count: 59,
      phoneNum: "",
      isShowCount: true,
      arr: [
        require("@/assets/img/integral-01.9386d4bf.png"),
        require("@/assets/img/integral-02.150d92a1.png"),
        require("@/assets/img/integral-03.9870f3f1.png"),
        require("@/assets/img/integral-04.afadcbdf.png"),
      ],
      code: "",
    };
  },
  computed: {
    ...mapState({
      isShowModal: (state) => state.loginModal.isShowModal,
    }),
  },
  methods: {
    ...mapMutations({
      changeModal: "loginModal/changeModal",
      changeIsLogined: "loginStatus/changeIsLogined",
    }),
    ...mapActions({
      asyncChangeIsShowToast: "showToast/asyncChangeIsShowToast",
      asyncChangeUserInfo: "userInfo/asyncChangeUserInfo",
    }),
    // 拼图成功
    onSuccess(times) {
      let ms = (times / 1000).toFixed(1);
      this.msg = "login success, 耗时 " + ms + "s";
    },
    // 拼图失败
    onFail() {
      this.onRefresh(); // 重新刷新拼图
    },
    // 拼图刷新
    onRefresh() {
      this.msg = "再试一次";
    },
    // 校验手机号和滑块
    check() {
      // 1 手机号校验
      if (!checkPhoneNum(this.phoneNum)) {
        this.asyncChangeIsShowToast({
          isShow: true,
          msg: "请输入正确的手机号!",
          type: "danger",
        });
        return;
      }

      // 2 滑块校验
      if (this.msg == "再试一次" || this.msg == "向右滑动") {
        this.asyncChangeIsShowToast({
          isShow: true,
          msg: "请滑动滑块!!",
          type: "danger",
        });
        return;
      }
      return true;
    },
    daojishi() {
      this.isShowCount = false;
      let timer = setInterval(() => {
        this.count--;
        if (this.count < 0) {
          this.count = 59;
          this.isShowCount = true;
          clearInterval(timer);
        }
      }, 1000);
    },
    // 验证码
    async getCode() {
      if (!this.check()) return;
      // 发送请求
      let res = await sendSMS({
        phone: this.phone,
      });
      console.log(res);
      if (!res) return;
      this.daojishi();
    },
    // 登录
    async checkFn() {
      if (!this.check()) return;
      if (!this.code) {
        this.asyncChangeIsShowToast({
          isShow: true,
          msg: "请输入验证码!!",
          type: "danger",
        });
        return;
      }
      let uuid = localStorage.getItem("uuid");
      let res = null;
      if (uuid) {
        res = await wxBindPhone({
          phone: this.phoneNum,
          verifyCode: this.code,
          uuid,
        });
      } else {
        res = await phoneRegin({
          phone: this.phoneNum,
          verifyCode: this.code,
        });
      }

      if (!res) return;
      this.changeModal(false);
      localStorage.setItem("x-auth-token", res["x-auth-token"]);
      this.changeIsLogined(true);
      this.asyncChangeIsShowToast({
        isShow: true,
        msg: "登录成功",
        type: "success",
      });
      this.asyncChangeUserInfo();
      uuid && this.$router.push(this.$route.path);
      localStorage.removeItem("uuid");
    },
    showQR() {
      this.isPhoneLogin = false;
      //获取二维码 并展示
      // 申请微信登录二维码
      let _this = this;
      new WxLogin({
        id: "weixin",
        appid: "wx67cfaf9e3ad31a0d", // 这个appid要填死
        scope: "snsapi_login",
        // 扫码成功后重定向的接口
        redirect_uri: "https://sc.wolfcode.cn/cms/wechatUsers/shop/PC",
        // state填写编码后的url
        state: encodeURIComponent(
          window.btoa("http://127.0.0.1:8080" + _this.$route.path)
        ),
        // 调用样式文件
        href: "data:text/css;base64,LmltcG93ZXJCb3ggLnRpdGxlLA0KLmltcG93ZXJCb3ggLmluZm8gew0KICAgIGRpc3BsYXk6IG5vbmU7DQp9",
      });
    },
  },
};
</script>

<style lang="less" scoped>
.modal {
  width: 100%;
  height: 100%;
  position: fixed;
  top: 0;
  left: 0;
  margin: auto;
  .mask {
    width: 100%;
    height: 100%;
    position: fixed;
    top: 0;
    left: 0;
    background-color: rgba(0, 0, 0, 0.3);
  }
  .loginBox {
    width: 555px;
    height: 423px;
    position: absolute;
    inset: 0;
    margin: auto;
    background-image: url("@/assets/img/login-box-bg.png");
    .close {
      height: 22px;
      width: 22px;
      position: absolute;
      top: 20px;
      right: 60px;
      cursor: pointer;
      background-image: url("@/assets/img/close.png");
    }
    .nav {
      text-align: center;
      margin-top: 50px;
      a {
        text-decoration: none;
        padding: 0 10px;
        color: gray;
        &.active {
          color: black;
        }
      }
    }
    .content {
      width: 355px;
      height: 300px;
      margin: 10px auto 0;
    }
    .phoneLogin {
      height: 100%;
      display: flex;
      flex-flow: column;
      .row {
        flex: 1;
        display: flex;
        align-items: center;
        input {
          flex: 1;
          height: 50px;
          padding-left: 10px;
          &:focus {
            outline: none;
          }
        }
        button {
          height: 50px;
          border: none;
          color: #fff;
          background: #0a328e;
          &.loginBtn {
            flex: 1;
          }
          &.codeBtn {
            width: 86px;
            margin-left: 10px;
          }
        }
      }
    }
    .WeChatLogin {
      text-align: center;
    }
  }
}

/deep/.slide-box {
  width: 100%;
  position: relative;
  box-sizing: border-box;
  canvas {
    position: absolute;
    left: 0;
    top: -120px;
    display: none;
    width: 100%;
    box-sizing: border-box;
  }
  .slide-verify-block {
    width: 85px;
    height: 136px;
  }
  .slide-verify-refresh-icon {
    top: -120px;
    display: none;
  }
  &:hover {
    canvas {
      display: block;
    }
    .slide-verify-refresh-icon {
      display: block;
    }
  }
}
</style>